import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "./assets/css/index.css";
import "./assets/css/iconfont.css";
import config from "./assets/js/config";
import axios from "axios";
import VueAxios from "vue-axios";
import ECharts from "vue-echarts";
import "echarts/lib/chart/line";
import "echarts/lib/chart/pie";
import "echarts/lib/chart/bar";
import "echarts/lib/chart/map";
import "echarts/lib/component/tooltip";
import "echarts/lib/component/legend";
import "echarts/lib/component/title";
import mavonEditor from "mavon-editor";
import "mavon-editor/dist/css/index.css";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import VueCalendarHeatmap from "vue-calendar-heatmap";
import dayjs from "dayjs";
import tagCloud from "./components/tag-cloud";
import * as authUtils from "./utils/authUtils"

Vue.prototype.config = config;
Vue.use(mavonEditor);
Vue.use(tagCloud);
Vue.use(VueCalendarHeatmap);
Vue.component("v-chart", ECharts);
Vue.use(VueAxios, axios);
Vue.use(ElementUI);
Vue.config.productionTip = false;
Vue.prototype.$moment = dayjs;

Vue.filter("date", function(value, formatStr = "YYYY-MM-DD") {
  return dayjs(value).format(formatStr);
});

Vue.filter("dateTime", function(value, formatStr = "YYYY-MM-DD HH:mm:ss") {
  return dayjs(value).format(formatStr);
});

NProgress.configure({
  easing: "ease", // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
});

router.beforeEach((to, from, next) => {
  NProgress.start();

  if(authUtils.isExpire()){
    store.commit("flushLoginDate");
  }else{
    store.commit("flushAll");
  }

  if (to.path == "/login") {
    if (store.state.userId != null && store.state.token != null) {
      next({ path: "/" });
    } else {
      next();
    }
  } else {
    if (store.state.userId != null && store.state.token != null) {
      next();
    } else {
      next({ path: "/login" });
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});


// 请求拦截器  [在请求头中添加token]
axios.interceptors.request.use(
  (config) => {

    if(authUtils.isExpire()){
      store.commit("flushLoginDate");
    }else{
      store.commit("removeToken");
    }

    if (store.state.token != null) {
      config.headers["token"] = store.state.token;

    }
    return config;
  }
  // (error) => {
  //   return Promise.reject(error);
  // }
);


// 响应拦截器
axios.interceptors.response.use(
  (response) => {

    // const code = response.data.code;
    // const message = response.data.message;

    return response;
  },
  (error) => {
    //获取返回的数据
    var data = (error.response || {}).data || {};
    var code = data.code || (error.response || error).status || {};
    var message = data.message || error.message;

    if(authUtils.isExpire()){
      store.commit("flushLoginDate");
      if (code !== 404 && code !== 500 && code !== 403) {
        store.commit("flushAll");
        router.push({ path: "/login" });
      }
    }else if(store.state.loginDate !== null){
      if(store.state.token === null){
        message = "当前登录状态已过期";
      }
      store.commit("flushAll");
      router.push({ path: "/login" });
    }

    Vue.prototype.$notify.error({
      title: "失败",
      message: message,
    });

    return Promise.reject(error);
  }
);

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
